package com.aner.tester.generator.param;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.aner.tester.config.AnerTesterConfig;
import com.aner.tester.generator.TypeManager;
import com.aner.tester.object.MethodObject;
import org.slf4j.Logger;
import com.aner.tester.utils.LoggerUtils;

import java.util.Date;

/**
 * parameter factory
 *
 * @author aner
 * @version 1.0
 * @date 2020/12/12 8:07
 */
public class ParameterFactory {

    protected static Logger logger = LoggerUtils.getLogger(ParameterFactory.class);

    private static ParameterGenerator generator;
    public static String generate(MethodObject methodObject) {
        if (methodObject.getContentType() == ContentType.FORM_DATA) {
            generator = new FormDataGenerator(methodObject);
        } else {
            generator = new PayloadGenerator(methodObject);
        }
        return formatResult(generator.generate());
    }

    public static String formatResult(Object object) {
        if (object instanceof Date) {
            return String.valueOf(((Date) object).getTime());
        }else if (TypeManager.basicContains(object.getClass().getSimpleName())) {
            return object.toString();
        } else {
            return JSON.toJSONStringWithDateFormat(object,
                    AnerTesterConfig.me().getDateFormat(),
                    SerializerFeature.UseSingleQuotes);
        }
    }
}
